package com.slkj.shilixiaoyuanapp.ui.user.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.CaseEntity;
import com.slkj.shilixiaoyuanapp.entity.MyWageEntity;
import com.slkj.shilixiaoyuanapp.entity.WagesNetEntity;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.my_wages_activity, title = "我的工资")
public class MyWagesActivity extends BaseActivity {
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.layout_calendar)
    RelativeLayout layoutCalendar;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.recycer)
    RecyclerView recycer;
    WageAdapter adapter;
    List<CaseEntity> datas = new ArrayList<>();
    //当前月份
    String nowMonth;
    @Override
    protected void init() {
        nowMonth = TimeUtils.getNy();
        tvTime.setText(nowMonth);
        adapter = new WageAdapter(datas);
        recycer.setLayoutManager(new LinearLayoutManager(this));
        recycer.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.HORIZONTAL));
        recycer.setAdapter(adapter);
        stateLayout.setOnReLoadListener(() -> getData(false));
        getData(false);
    }

    private void getData(boolean isShowDialog) {
        HttpHeper.get().userService()
                .getWages(nowMonth)
                .map(a -> {
                    datas.clear();
                    if (a.getStatus().equals(HttpConfig.RESPONSE_OK) && a.getData() != null && a.getData().getList()!=null){
                        List<WagesNetEntity> backData =  a.getData().getList();
                        for (WagesNetEntity wagesNetEntity : backData) {
                            datas.add(new CaseEntity(wagesNetEntity.getTitle(),CaseEntity.TYPE_TITLE));
                            if (!wagesNetEntity.getCasex().isEmpty()){
                                datas.addAll(wagesNetEntity.getCasex());
                            }
                        }
                    }
                    return a;
                })
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<MyWageEntity>(isShowDialog,this){
                    @Override
                    public void onCallBackSuccess(MyWageEntity data) {
                        if (datas.isEmpty()){
                            stateLayout.showEmptyView();
                        }else {
                            stateLayout.showContentView();
                        }
                        adapter.notifyDataSetChanged();
                        tvAll.setText(data.getTotal());

                    }

                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        stateLayout.showErrorView();

                    }
                });
    }


    @OnClick(R.id.layout_calendar)
    public void onViewClicked() {
        PickerViewProvider.getDayTimePickerBefor(this,(date,v) ->{
            nowMonth = TimeUtils.getNy(date);
            tvTime.setText(nowMonth);
            getData(true);
        },new boolean[]{true,true,false,false,false,false}, new Date()).show();
    }
    public class WageAdapter extends BaseMultiItemQuickAdapter<CaseEntity, BaseViewHolder> {
        public WageAdapter(List<CaseEntity> data) {
            super(data);
            addItemType(CaseEntity.TYPE_TITLE, R.layout.item_user_wage_title);
            addItemType(CaseEntity.TYPE_CONTENT, R.layout.item_user_wage_content);
        }
        @Override
        protected void convert(BaseViewHolder helper, CaseEntity item) {
            switch (helper.getItemViewType()) {
                case CaseEntity.TYPE_TITLE:
                    helper.setText(R.id.tv_title,item.getTitle());
                    break;
                case CaseEntity.TYPE_CONTENT:
                    helper.setText(R.id.tv_title,item.getTitle());
                    helper.setText(R.id.tv_content,item.getMonery());
                    break;
            }
        }
    }
}
